package com.yinhe.music.yhmusic.dialog;

import android.content.Context;
import android.support.design.widget.BottomSheetDialog;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.constants.IConstants;
import com.yinhe.music.yhmusic.model.Music;
import com.yinhe.music.yhmusic.utils.Preferences;

import java.util.List;

public class MusicQualityDialog {

    public interface MusicQualityDialogListener {
        void playQualityMusic(int drawable, String quality);
    }

    private BottomSheetDialog mDialog;
    private Context mContext;
    private Music mMusic;
    private ImageView selectNormal, selectHq, selectSq;
    private LinearLayout normal, hq, sq;
    private MusicQualityDialogListener mListener;


    public MusicQualityDialog(Context mContext, Music mMusic, MusicQualityDialogListener mListener) {
        this.mContext = mContext;
        this.mMusic = mMusic;
        this.mListener = mListener;
        init();
    }

    public void show() {
        if (mDialog != null) {
            mDialog.show();
        }
    }

    private void init() {
        View sheetDialogView = View.inflate(mContext, R.layout.quality_sheet_dialog, null);
        mDialog = new BottomSheetDialog(mContext);
        mDialog.setContentView(sheetDialogView);
        mDialog.setOnDismissListener(dialog -> mDialog = null);
        selectNormal = sheetDialogView.findViewById(R.id.select_normal);
        selectHq     = sheetDialogView.findViewById(R.id.select_hq);
        selectSq     = sheetDialogView.findViewById(R.id.select_sq);
        normal       = sheetDialogView.findViewById(R.id.normal_layout);
        hq           = sheetDialogView.findViewById(R.id.hq_layout);
        sq           = sheetDialogView.findViewById(R.id.sq_layout);

        switch (Preferences.getQuality()) {
            case IConstants.Common:
                selectNormal.setVisibility(View.VISIBLE);
                break;
            case IConstants.HQ:
                selectHq.setVisibility(View.VISIBLE);
                break;
            case IConstants.SQ:
                selectSq.setVisibility(View.VISIBLE);
                break;
            default:
                break;
        }

        setQualityLevel();
    }

    private void setQualityLevel() {
        if (sq != null && hq != null) {
            TextView textView = sq.findViewById(R.id.pay_textview);
            TextView hqText = hq.findViewById(R.id.hq_pay_textview);
            textView.setCompoundDrawables(null, null, mMusic.price <= 0
                    ? null : mContext.getResources().getDrawable(R.drawable.ic_pay),
                    null);
            hqText.setCompoundDrawables(null, null, mMusic.price <= 0
                    ? null : mContext.getResources().getDrawable(R.drawable.ic_pay), null);
        }

        List<String> list = mMusic.getQualityType();
        for (String i : list) {
            switch (i) {
                case IConstants.Common:
                    normal.setAlpha((float) 1);
                    normal.setOnClickListener(v -> {
                        mListener.playQualityMusic(R.drawable.ic_regular_long, IConstants.Common);
                        setQualitySelect(selectNormal, selectHq, selectSq);
                        dimissDialog();
                    });
                    break;
                case IConstants.HQ:
                    hq.setAlpha((float) 1);
                    hq.setOnClickListener(view -> {
                        if (Preferences.getIsVip()) {
                            mListener.playQualityMusic(R.drawable.ic_hq_long, IConstants.HQ);
                            setQualitySelect(selectHq, selectSq, selectNormal);
                        } else {
                            checkSong(R.drawable.ic_hq_long, IConstants.HQ);
                        }
                        dimissDialog();
                    });
                    break;
                case IConstants.SQ:
                    sq.setAlpha((float) 1);
                    sq.setOnClickListener(view -> {
                        if (Preferences.getIsVip()) {
                            mListener.playQualityMusic(R.drawable.ic_sq_long, IConstants.SQ);
                            setQualitySelect(selectSq, selectHq, selectNormal);
                        } else {
                            checkSong(R.drawable.ic_sq_long, IConstants.SQ);
                        }
                        dimissDialog();
                    });
                    break;
                default:
                    break;
            }
        }
    }

    private void setQualitySelect(ImageView selectImage, ImageView unSelect, ImageView unSelect2) {
        selectImage.setVisibility(View.VISIBLE);
        unSelect.setVisibility(View.GONE);
        unSelect2.setVisibility(View.GONE);
    }

    private void dimissDialog() {
        if (mDialog != null) {
            mDialog.dismiss();
        }
    }

    private void checkSong( int drawable, final String quality) {
        if (mMusic.getPurchaseStatus() == 0) {
            showBuyDialog();
        } else {
            mListener.playQualityMusic(drawable, quality);
            setQualitySelect(selectSq, selectHq, selectNormal);
        }
    }

    private void showBuyDialog() {
        BuyDialog dialog = new BuyDialog(mContext, mMusic);
        dialog.show();
    }
}
